package com.example.demo02.task;

import com.example.demo02.dto.ValueChangeDTO;
import com.example.demo02.enums.ChangeKeyEnum;
import com.example.demo02.listener.event.ValueChangeEvent;
import com.example.demo02.utils.EventUtils;
import com.example.demo02.utils.ParamUtil;
import com.example.demo02.utils.ParamUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class HeartThrobTask {
    @Autowired
    EventUtils eventUtils;

    @Scheduled(cron = "0/2 * * * * ? ")
    public void heart1() {
        ValueChangeDTO valueChangeDTO = new ValueChangeDTO();
        valueChangeDTO.setOldValue(ParamUtil.getOnline().toString());
        ParamUtil.setOnline(!ParamUtil.getOnline());

        valueChangeDTO.setNewValue(ParamUtil.getOnline().toString());
        valueChangeDTO.setKeyName(ChangeKeyEnum.on_line);
        eventUtils.publishEvent(new ValueChangeEvent(valueChangeDTO, true));

    }
}
